﻿using SysSurv.bll;
using SysSurv.dtl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;

namespace SysSurv
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();            
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            refreshLists();
        }
        private void refreshLists() {
            lstMySurveys.DataSource = null;
            lstAvSurveys.DataSource = null;
            try
            {
                lstMySurveys.DataSource = Wiii.getInstance().getUserSurveys();
                lstAvSurveys.DataSource = Wiii.getInstance().getAvSurveys();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void btnAddQuestion_Click(object sender, EventArgs e)
        {
            if (txtQuestion.Text != "")
            {
                Question q = new Question(txtQuestion.Text, new List<Answer>());
                lstQuestions.Items.Add(q);
                txtQuestion.Text = "";
            }
            else {
                MessageBox.Show("Debe ingresar un texto para la pregunta");
            }
        }

        private void tabCreateSurvey_Click(object sender, EventArgs e)
        {

        }

        private void btnDelQuestion_Click(object sender, EventArgs e)
        {
            try
            {
                lstQuestions.Items.Remove(lstQuestions.SelectedItem);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnAddAnswer_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtAnswer.Text != "")
                {
                    Answer a = new Answer(txtAnswer.Text);
                    Question q = (Question)lstQuestions.SelectedItem;
                    q.lstAnswers.Add(a);
                    lstAnswers.DataSource = null;
                    lstAnswers.DataSource = q.lstAnswers;
                    txtAnswer.Text = "";
                }
                else
                {
                    MessageBox.Show("Debe ingresar un texto para la respuesta.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDelAnswer_Click(object sender, EventArgs e)
        {
            try
            {
                Question q = (Question)lstQuestions.SelectedItem;
                q.lstAnswers.Remove((Answer)lstAnswers.SelectedItem);
                lstAnswers.DataSource = null;
                lstAnswers.DataSource = q.lstAnswers;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void lstQuestions_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstQuestions.SelectedIndex >= 0)
            {
                Question q = (Question)lstQuestions.SelectedItem;
                lstAnswers.DataSource = q.lstAnswers;
            }
            else {
                lstAnswers.DataSource = null;
            }
        }

        private void btnClean_Click(object sender, EventArgs e)
        {
            cleanForm();
        }

        private void btnAddSurvey_Click(object sender, EventArgs e)
        {
            if (txtName.Text != "")
            {
                if (lstQuestions.Items.Count >= 0)
                {
                    if (!less2())
                    {
                        try
                        {
                            Question q;
                            object[] a = new object[lstQuestions.Items.Count];
                            lstQuestions.Items.CopyTo(a, 0);
                            List<Question> lstQ = new List<Question>();
                            foreach (object o in a)
                            {
                                q = (Question)o;
                                lstQ.Add(q);
                            }
                            Survey s = new Survey(txtName.Text, lstQ, Wiii.getInstance().UsrLog);
                            Wiii.getInstance().createSurvey(s);
                            cleanForm();
                            lstMySurveys.DataSource = null;
                            lstMySurveys.DataSource = Wiii.getInstance().getUserSurveys();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Todas las preguntas de la encuesta deben tener al menos dos respuestas");
                    }
                }
                else
                {
                    MessageBox.Show("Debe ingresar al menos una pregunta para crear una encuesta");
                }
            }
            else {
                MessageBox.Show("Debe ingresar un nombre para la nueva encuesta");
            }
        }
        private bool less2() {
            bool aux = false;
            try
            {
                foreach (Question q in lstQuestions.Items)
                {
                    if (q.lstAnswers.Count < 2)
                    {
                        aux = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return aux;
        }
        private void cleanForm() {
            lstQuestions.Items.Clear();
            lstAnswers.DataSource = null;
            txtName.Text = "";
            txtAnswer.Text = "";
            txtQuestion.Text = "";
        }

        private void lblSelectSurvey_Click(object sender, EventArgs e)
        {

        }

        private void lstMySurveys_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void tabMySurveys_Click(object sender, EventArgs e)
        {
            
        }

        private void tabAvailables_Click(object sender, EventArgs e)
        {
            
        }

        private void tacMain_SelectedIndexChanged(object sender, EventArgs e)
        {
            refreshLists();
        }

        private void btnFill_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstAvSurveys.SelectedIndex >= 0)
                {
                    long pId = ((Survey)lstAvSurveys.SelectedItem).i64Id;
                    RestClient client = new RestClient("http://localhost:8080/SpringSurv/surveys/" + pId, HttpVerb.GET, "", true);
                    string response = client.MakeRequest();
                    Wiii.getInstance().srvAux = new JavaScriptSerializer().Deserialize<Survey>(response);
                    frmFillSurvey ffs = new frmFillSurvey();
                    ffs.ShowDialog();
                }
                else {
                    MessageBox.Show("Debe seleccionar una encuesta");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstMySurveys.SelectedIndex >= 0)
                {
                    long pId = ((Survey)lstMySurveys.SelectedItem).i64Id;
                    RestClient client = new RestClient("http://localhost:8080/SpringSurv/surveys/delete/" + pId, HttpVerb.DELETE, "", false);
                    client.MakeRequest();
                    refreshLists();
                }
                else {
                    MessageBox.Show("Debe seleccionar la encuesta que desea borrar");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnResults_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstMySurveys.SelectedIndex >= 0)
                {
                    long pId = ((Survey)lstMySurveys.SelectedItem).i64Id;
                    RestClient client = new RestClient("http://localhost:8080/SpringSurv/surveys/" + pId, HttpVerb.GET, "", true);
                    string response = client.MakeRequest();
                    Wiii.getInstance().srvAux = new JavaScriptSerializer().Deserialize<Survey>(response);
                    frmResults ffr = new frmResults();
                    ffr.ShowDialog();
                }
                else
                {
                    MessageBox.Show("Debe seleccionar una encuesta");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
